package site.brood.mall.pms.brand;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import site.brood.mall.util.ToolKit;
import java.util.List;

/**
 * 品牌管理Controller
 */
//tags:该类作用说明
//value:没用，不用配
@Api(tags = "品牌")    //Swagger控制器注解
@RestController
@RequestMapping("/brand")
@Slf4j
public class PmsBrandController {

    @Trace  //skywalking采集注解
    @Tags({ @Tag(key = "output", value = "returnedObj") })  //采集返回值
    @SaCheckLogin
    @ApiOperation("查询所有品牌列表")     //Swagger方法注解
    @ApiResponses(value = {         //Swagger返回注解
            @ApiResponse(code=200,message = "成功"),
            @ApiResponse(code=500,message = "失败"),
            @ApiResponse(code=403,message = "未授权")
    })
    @GetMapping("/list_all")
    public AjaxJson list(){
        List<PmsBrand> list = ToolKit.pmsBrandService.lambdaQuery().list();
        return AjaxJson.success(list);
    }

    @SaCheckLogin
    @ApiOperation("分页查询品牌列表")     //Swagger方法注解
    @GetMapping("/list_by_page")
    public AjaxJson listByPage(@RequestParam(value = "pageIndex",defaultValue = "1")
                               @ApiParam("页码") Integer pageIndex,
                               @RequestParam(value = "pageSize", defaultValue = "10")
                               @ApiParam("每页数量") Integer pageSize){
        QueryWrapper<PmsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        Page<PmsBrand> page = new Page<>(pageIndex, pageSize);
        IPage<PmsBrand> iPage = ToolKit.pmsBrandMapper.selectPage(page, queryWrapper);
        return AjaxJson.success(iPage);
    }

    @ApiOperation("查一个品牌")     //Swagger方法注解
    @GetMapping("/one")
    public AjaxJson one(@ApiParam("id") String id){
        PmsBrand pmsBrand = ToolKit.pmsBrandService.lambdaQuery().eq(PmsBrand::getId, id).one();
        return AjaxJson.createByNull(pmsBrand);
    }

    @ApiOperation("保存")
    @ApiImplicitParams({    //参数说明
            @ApiImplicitParam(name = "pmsBrand",value = "品牌对象",required = true,paramType = "body",dataType = "String")
    })
    @PostMapping("save")
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson save(@RequestBody PmsBrand pmsBrand){
        boolean isSuccess = ToolKit.pmsBrandService.saveOrUpdate(pmsBrand);
        if(isSuccess)
            return AjaxJson.success(pmsBrand);
        else
            return AjaxJson.error();
    }

    @ApiOperation("条件查询品牌列表")     //Swagger方法注解
    //name:参数名
    //value:参数说明
    //paramType:参数放哪里
    //1)header 请求头 2)query url或form表单 3)path url中
    //defaultValue:默认值
    @ApiImplicitParams({    //参数说明
            @ApiImplicitParam(name = "name",value = "名称",required = true,paramType = "query",dataType = "String")
    })
    @GetMapping("/query")
    public AjaxJson query(String name){
        List<PmsBrand> list = ToolKit.pmsBrandService.lambdaQuery()
                .like(PmsBrand::getName,name)
                .list();
        return AjaxJson.success(list);
    }

    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true, paramType = "form",dataType = "String")
    })
    @PostMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson delete(String id){
        boolean isRemoved = ToolKit.pmsBrandService.lambdaUpdate()
                .eq(PmsBrand::getId, id)
                .remove();
        return AjaxJson.createByBoolean(isRemoved);
    }
}

